/*
 * MatchRoom.java
 *
 * Distributed Systems: Project 2
 *
 */

package unimelb.distSystems.communications;

import unimelb.distSystems.communications.CORBA.*;

import java.util.*;


public class MatchRoom {
    private long matchroom_id;
    private String name;
    private int max_players;
    private long track_id;
    private long host_id;
    private boolean raceInProgress;

    private List players;
    private List chat;


    public MatchRoom(){
        players = new ArrayList();
        chat = new ArrayList();
    }

    public MatchRoom(ServiceMatchRoom room){
        matchroom_id = room.matchroom_id;
        name = room.name;
        max_players = room.max_players;
        track_id = room.track_id;
        host_id = room.host_id;
        raceInProgress = room.raceInProgress;

        players = new ArrayList();
        chat = new ArrayList();
    }

    public ServiceMatchRoom getServiceMatchRoom(){
        ServiceMatchRoom room = new ServiceMatchRoom();
        room.matchroom_id = matchroom_id;
        room.name = name;
        room.max_players = max_players;
        room.track_id = track_id;
        room.host_id = host_id;
        room.raceInProgress = raceInProgress;

        return room;
    }

    public void generateID(){
        this.matchroom_id = name.hashCode();
    }

    public void adjustID(){
        this.matchroom_id++;
    }

    public long getMatchroom_id() {
        return matchroom_id;
    }

    public void setMatchroom_id(long matchroom_id) {
        this.matchroom_id = matchroom_id;
    }

    public boolean inRoom(long playerID){
        ListIterator li = players.listIterator();

        while(li.hasNext()){
            Player player = (Player)li.next();
            if(player.getAcount().getUserID() == playerID){
                return true;
            }
        }
        return false;
    }

    public int getMax_players() {
        return max_players;
    }

    public void setMax_players(int max_players) {
        this.max_players = max_players;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getTrack_id() {
        return track_id;
    }

    public void setTrack_id(long track_id) {
        this.track_id = track_id;
    }

    public List getChat() {
        return chat;
    }

    public String[] getChatArray() {
        String[] list = new String[chat.size()];
        ListIterator li = chat.listIterator();

        int index = 0;
        while(li.hasNext()){
            index = li.nextIndex();
            ServiceMessage message = (ServiceMessage)li.next();
            list[index] = message.driver + ": " + message.message;
        }
        return list;
    }

    public void setChat(List chat) {
        this.chat = chat;
    }

    public List getPlayers() {
        return players;
    }

    public ServicePlayer[] getPlayersArray() {
        ServicePlayer[] list = new ServicePlayer[players.size()];
        ListIterator li = players.listIterator();

        int index = 0;
        while(li.hasNext()){
            index = li.nextIndex();
            Player player = (Player)li.next();
            list[index] = player.getServicePlayer();
        }

        return list;
    }

    public Player getPlayer(long playerID){
        ListIterator li = players.listIterator();

        while(li.hasNext()){
            Player player = (Player) li.next();
            if(player.getAcount().getUserID() == playerID){
                return player;
            }
        }
        return null;
    }

    public void setPlayers(List players) {
        this.players = players;
    }
    

    public void addMessage(ServiceMessage message){
        chat.add(message);
    }

    public boolean addPlayer(Player player){
        if(!inRoom(player.getAcount().getUserID())){
            players.add(player);
            return true;
        }
        return false;
    }

    public boolean removePlayer(Player player){
        if(inRoom(player.getAcount().getUserID())){
            players.remove(player);
            return true;
        }
        return false;
    }

    public boolean removePlayer(long playerID){
        ListIterator li = players.listIterator();

        int index = 0;
        while(li.hasNext()){
            index = li.nextIndex();
            Player player = (Player) li.next();
            if(player.getAcount().getUserID() == playerID){
                players.remove(index);
                return true;
            }
        }
        return false;
    }

    public long getHost_id() {
        return host_id;
    }

    public void setHost_id(long host_id) {
        this.host_id = host_id;
    }

    public boolean isRaceInProgress() {
        return raceInProgress;
    }

    public void setRaceInProgress(boolean raceInProgress) {
        this.raceInProgress = raceInProgress;
    }

    public void resetReady(){
        ListIterator li = players.listIterator();

        while(li.hasNext()){
            Player player = (Player) li.next();
            player.setReady(false);
        }
    }

    public boolean onYourMarks(){
        ListIterator li = players.listIterator();

        boolean onYourMarks = true;
        while(li.hasNext()){
            Player player = (Player)li.next();
            if(!player.isMark()){
                onYourMarks = false;
            }
        }
        return onYourMarks;
    }


}
